﻿using Dapper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.PurchaseOrder;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.PurchaseOrder
{
    public class WhPoInboundServices : IWhPoInboundServices
    {

        public WhPoInboundServices(IBaseRepository db, IWhPoInboundRepository or)
        {

            Or = or;
            Db = db;
        }
        public IWhPoInboundRepository Or { get; }
        public IBaseRepository Db { get; }

        //新增
        public async Task<bool> AddAsync(WhPoInbound model, List<WhPoInboundItem> list)
        {
            var result = await Or.AddWhPoInbound(model, list);
            return result;
        }


        /// <summary>
        /// 新增采购入库
        /// </summary>
        /// <param name="model"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<bool> AddwhPoInbound(WhPoInbound model, List<WhPoInboundItem> list)
        {
            var result = await Or.AddWhPoInbound(model, list);
            return result;
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> Delete(int[] Ids)
        {
            var result = await Or.Delete(Ids);
            return result;
        }

        /// <summary>
        /// 删除单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> Delete(int Id)
        {
            var result = await Or.Delete(Id);
            return result;
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<WhPoInbound>> GetAllAsync()
        {
            string sql = "select * from WhPoInbound";
            var list = await Db.QueryAllAsync<WhPoInbound>(sql);
            return list;
        }

        /// <summary>
        /// 分页获取 / 根据条件查询数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Number"></param>
        /// <param name="VendorName"></param>
        /// <param name="WarehouseName"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public async Task<IEnumerable<WhPoInbound>> GetAsync(int PageIndex, int PageSize, string Number, string VendorName, string WarehouseName,string Status)
        {
            string sql = "select * from WhPoInbound where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(VendorName))
            {
                sql += " and VendorName like @VendorName";
            }

            if (!string.IsNullOrEmpty(WarehouseName))
            {
                sql += " and WarehouseName like @WarehouseName";
            }


            if (!string.IsNullOrEmpty(Status) && !Status.Equals("全部"))
            {
                sql += " and Status = @Status";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";


            var list = await Db.QueryAsync<WhPoInbound>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", VendorName = $"%{VendorName}%", WarehouseName = $"%{WarehouseName}%", Status });
            return list;
        }


        //获取总条数
        public async Task<int> GetCountAsync(string VendorId, string VendorName, string WarehouseName, string Status)
        {
            string sql = "select count(*) from WhPoInbound where 1=1 ";
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }

            if (!string.IsNullOrEmpty(VendorName))
            {
                sql += " and VendorName like @VendorName";
            }

            if (!string.IsNullOrEmpty(WarehouseName))
            {
                sql += " and WarehouseName like @WarehouseName";
            }
            if (!string.IsNullOrEmpty(Status) && !Status.Equals("全部"))
            {
                sql += " and Status = @Status";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { VendorId = $"%{VendorId}%", VendorName = $"%{VendorName}%", WarehouseName = $"%{WarehouseName}%", Status });
            return count;
        }

        public async Task<IEnumerable<WhPoInboundItemDto>> GetWhPoInboundItemById(string WhPoInboundId, string WarehouseId)
        {
            var list = await Db.QueryAsync<WhPoInboundItemDto>(@"select it.Spec,it.UnitOfMeasure,wp.*,it.Id as ItemId,it.ItemCode,it.ItemName,w.WarehouseId,
                    it.PurchasePrice,(isk.StockCount-isk.SalesCount) as CurrCount
                    from WhPoInboundItem wp
                    inner join WhPoInbound w on w.Id=wp.WhPoInboundId
                    inner join Item it on it.Id = wp.ItemId
                    inner join ItemStock isk on isk.ItemId = it.Id and isk.WareHouseId = @WareHouseId
                    where wp.WhPoInboundId =@WhPoInboundId", new { WhPoInboundId, WarehouseId });
            return list;
        }

        public async Task<bool> UpdateAsync(WhPoInbound model, List<WhPoInboundItem> list)
        {
            var result = await Or.UpdateWhPoInbound(model, list);
            return result;
        }


        public async Task<bool> UpdateStatus(long Id, int Status)
        {
            string sql = $"update WhPoInbound set Status=@Status where Id =@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }
    }
}
